package com.leetcode.no860;

import java.util.HashMap;

public class Solution {
    public boolean lemonadeChange(int[] bills) {
        HashMap<Integer, Integer> account = new HashMap();
        account.put(5, 0);
        account.put(10, 0);
        account.put(20, 0);

//        int unit = 5;
        for (int i = 0; i < bills.length; i++) {
            switch (bills[i]) {
                case 5:
                    account.put(5, account.get(5) + 1);
                    break;
                case 10:
                    if (account.get(5) < 1) {
                        return false;
                    }

                    account.put(5, account.get(5) - 1);
                    break;
                case 20:
                    // 先看看有没有一张10块钱
                    if (account.get(10) < 1) {
                        // 用5块钱的补上去
                        if (account.get(5) < 3) {
                            return false;
                        }
                        // 能补上
                        account.put(5, account.get(5) - 3);
                        break;
                    }

                    // 有一张10块钱
                    if (account.get(5) < 1) {
                        return false;
                    }

                    account.put(10, account.get(10) - 1);
                    account.put(5, account.get(5) - 1);

                    break;
                default:
                    break;
            }
        }
        return true;
    }


    public boolean lemonadeChange01(int[] bills) {
        int five = 0, ten = 0;
        for (int bill : bills) {
            if (bill == 5) {
                five++;
            } else if (bill == 10) {
                if (five == 0) {
                    return false;
                }
                five--;
                ten++;
            } else {
                if (five > 0 && ten > 0) {
                    five--;
                    ten--;
                } else if (five >= 3) {
                    five -= 3;
                } else {
                    return false;
                }
            }
        }
        return true;
    }


}
